import 'dart:async';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:fluttermarketingplus/constant/global_data.dart';

//用户登陆,不带token
Future request(context, url, formData) async {
  Response response;
  try {
    Dio dio = new Dio();
    dio.options.contentType = ContentType.parse('application/json') as String;
    dio.options.connectTimeout = 15000;
    if (formData == null) {
      response = await dio.post(url);
    } else {
      response = await dio.post(url, data: formData);
    }
    if (response.statusCode == 200) {
      return response.data;
    } else if (response.statusCode == 500) {
      //此处为示例，可以多捕获一些状态吗
    } else {
      throw Exception('后端接口异常!');
    }
  } catch (e) {
    throw Exception('服务器端异常:${e}');
  }
}

//工位请求,订单请求，预约记录，用户登出，带token,获取灯控人是否离开
Future seatRequest(context, url, {formData}) async {
  Response response;
  String token = '';
  token = GlobalData.prefs.getString('counter');
  try {
    Dio dio = new Dio();
    dio.options.contentType = ContentType.parse('application/json') as String;
    dio.options.headers["Authorization"] = 'token_${token.toString()}';
    dio.options.connectTimeout = 15000;
    if (formData == null) {
      response = await dio.post(url);
    } else {
      response = await dio.post(url, data: formData);
    }
    if (response.statusCode == 200) {
      return response.data;
    }
  } catch (e) {
    throw Exception('捕获网络异常：${e}!');
  }
}

//get获取用户信息
Future getUserInfo(context, url, token, {formData}) async {
  try {
    Response response;
    Dio dio = new Dio();
    dio.options.headers["Authorization"] = 'token_${token.toString()}';
    dio.options.contentType = ContentType.parse('application/json') as String;
    dio.options.connectTimeout = 15000;
    if (formData == null) {
      response = await dio.get(url);
    } else {
      response = await dio.get(url);
    }
    if (response.statusCode == 200) {
//      print('返回的数据为：${response.data}');
      return response.data;
    } else {
      print('使用Get方法获取服务器异常:响应码为:${response.statusCode}');
    }
  } catch (e) {
    print('获取用户信息,当前异常为:${e.toString()}');
    return print(e);
  }
}

//不带参数的get请求
Future getRequest(String url, String sessionId) async {
  try {
    Response response;
    Dio dio = Dio();
    dio.options.headers['sessionId'] = sessionId;
    dio.options.contentType = ContentType.parse('application/json') as String;
    dio.options.connectTimeout = 15000;
    response = await dio.get(url);
    if (response.statusCode == 200) {
      return response.data;
    } else if (response.statusCode == 500) {
      //接着往下写 ....
    } else {
      print('不带参数的get请求 异常:响应码:${response.statusCode}');
    }
  } catch (e) {
    print('不带参数的get请求抛出异常，异常为:${e}');
  }
}

//带参数的post请求
Future postRequest(String url, formData, String sessionId) async {
  try {
    Response response;
    Dio dio = Dio();
    dio.options.headers['sessionId'] = sessionId;

    dio.options.contentType = ContentType.parse('application/json') as String;
    dio.options.connectTimeout = 15000;
    response = await dio.post(url, data: formData);
    if (response.statusCode == 200) {
      return response.data;
    } else {
      print('带参数的post请求异常:响应码为:${response.statusCode}');
    }
  } catch (e) {
    print('带参数的post请求抛出异常，异常为:${e}');
  }
}
